﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using RimWorld;
using Verse;
using static UnityEngine.GraphicsBuffer;

namespace ARMIR { 
    public class HediffComp_mindCrushWorker : HediffComp {
        public HediffCompProperties_mindCrushWorker Props => (HediffCompProperties_mindCrushWorker)props;
        private int gapExplosive;
        private int explosiveTimes;
        private Map map;
        public override void CompPostMake()
        {
            base.CompPostMake();
            gapExplosive = 0;
            explosiveTimes = 0;
            map = parent.pawn.Map;
        }
        public override void CompExposeData()
        {
            base.CompExposeData();
            Scribe_Values.Look(ref gapExplosive, "爆破间隔", 0);
            Scribe_Values.Look(ref explosiveTimes, "爆破次数", 0);
        }
        public override void CompPostTick(ref float severityAdjustment)
        {
            base.CompPostTick(ref severityAdjustment);
            //冷却期间中且尚未爆破完全
            if (gapExplosive < Props.gapEachExplosive && explosiveTimes < Props.radiusMax) {
                gapExplosive++;
                if (gapExplosive >= Props.gapEachExplosive) {
                    gapExplosive = 0;
                    explosiveTimes++;
                    doExplosive(explosiveTimes);
                }
            }
        }
        private void doExplosive(int explosiveRadius) { 
            IntVec3 Position = parent.pawn.Position;
            List<Thing> ignorePawns = new List<Thing>();
            ignorePawns.Add(parent.pawn);
            GenExplosion.DoExplosion(
                Position,
                map,
                radius: explosiveRadius,
                DamageDefOf.Crush,
                instigator: parent.pawn,
                damAmount: (int)Props.damageAmount / 2,
                armorPenetration: (int)Props.armorPenetration,
                damageFalloff: false, //伤害随距离衰减开启
                screenShakeFactor: 0.8f, //屏幕震动强度(默认为1
                explosionSound: DefDatabase<SoundDef>.GetNamed("ARMIR_fakeEmpty"),
                ignoredThings: ignorePawns
            );

            List<Thing> ExplosiveIgnoredPawns = new List<Thing>();
            ExplosiveIgnoredPawns = parent.pawn.Map.mapPawns.AllPawnsSpawned.Where(p => p.Faction == Faction.OfPlayer && p.Position.DistanceTo(Position) < explosiveRadius + 2).OfType<Thing>().ToList();
            ExplosiveIgnoredPawns.AddRange(parent.pawn.Map.listerThings.ThingsMatching(ThingRequest.ForGroup(ThingRequestGroup.HaulableEver)).Where(p => p.Position.DistanceTo(Position) < explosiveRadius + 2));
            ExplosiveIgnoredPawns.AddRange(parent.pawn.Map.listerThings.ThingsMatching(ThingRequest.ForGroup(ThingRequestGroup.Corpse)).Where(p => p.Position.DistanceTo(Position) < explosiveRadius + 2));
            ExplosiveIgnoredPawns.Add(parent.pawn);

            GenExplosion.DoExplosion(
                Position,
                map,
                radius: explosiveRadius,
                DamageDefOf.Crush,
                instigator: parent.pawn,
                damAmount: (int)Props.damageAmount * 3,
                armorPenetration: (int)Props.armorPenetration,
                damageFalloff: false, //伤害随距离衰减开启
                screenShakeFactor: 0.8f, //屏幕震动强度(默认为1
                explosionSound: DefDatabase<SoundDef>.GetNamed("ARMIR_fakeEmpty"),
                ignoredThings: ExplosiveIgnoredPawns
            );
        }
    }
}
